// 双向链表
class Node {
    constructor(data) {
        this.data = data
        this.prev = null
        this.next = null
    }
}

class DoubleLinkList {
    size = 0
    head = null
    tail = null

    // 在尾部插入一个元素
    append(data) {
        const node = new Node(data)
        if (this.size === 0) { // 证明其实没有数据
            this.head = node
            this.tail = node
        } else {
            node.prev = this.tail
            this.tail.next = node
            this.tail = node
        }
        this.size++
    }

    // 在任意位置插入一个元素
    insertIndex(index, data) {
        const node = new Node(data)
        if(index < 0 || index > this.size) {
            return false
        }
        if (index === 0 || index ===1 ) { // 在头部位置插入
            node.next = this.head
            this.head.prev = node
            this.head = node
        } 
        // 在中间位置插入
        if (index > 1 && index <= this.size) {
            let current = this.head
            let previous = null
            for (let i = 1; i < index; i++) {
                previous = current
                current = current.next
            }
            previous.next = node
            node.prev = previous
            node.next = current
            current.prev = node
        }
        this.size++
        return true
    }

    isEmpty() {
        return this.size == 0 ? true : false
    }

    // 删除
    delIndex(index) {
        if (this.size === 0) {
            return 
        }
        if (index <= 0 || index > this.size) {
            return 
        }
        if (index === 1) { 
            this.head = this.head.next
            this.head.prev = null // 断开原链接
        }
        if (index === this.size) {
            this.tail.prev.next = null
            this.tail = this.tail.prev
        }
        if (index > 1 && index < this.size) {
            let current = this.head 
            let previous = null
            for (let i = 1; i < index; i++) {
                previous = current
                current = current.next
            }
            previous.next = current.next
            current.next.prev = previous
        }
        this.size--
    }



    getSize(){
        return this.size
    }

    print(){
        if (this.size === 0) {
            console.log("这是个空链表");
            return
        }
        let node = this.head
        while(node) {
           console.log(node.data);
           node = node.next
        }
    }

}

const doubleLinkList = new DoubleLinkList()
doubleLinkList.append(1)
doubleLinkList.append(2)
doubleLinkList.append(3)

doubleLinkList.print()
console.log("链表长度为;", doubleLinkList.getSize());

doubleLinkList.insertIndex(2, 0)
doubleLinkList.print()

console.log("链表长度为;", doubleLinkList.getSize());
doubleLinkList.delIndex(4)
doubleLinkList.print()

console.log("链表长度为;", doubleLinkList.getSize());
doubleLinkList.delIndex(2)
doubleLinkList.print()


console.log("链表长度为;", doubleLinkList.getSize());
doubleLinkList.delIndex(1)
doubleLinkList.print()

console.log("链表长度为;", doubleLinkList.getSize());
doubleLinkList.delIndex(0)
doubleLinkList.print()
